﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace mySocket
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            int port = 2000;
            IPEndPoint ipend = new IPEndPoint(ip, port);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Console.WriteLine("与服务器连接至...");
            socket.Connect(ipend);
            Console.WriteLine("连接服务器成功");
            //发送和接收数据
            //NetworkStream ns = new NetworkStream(socket);
            string sendStr = "你好，我是客户端";
            byte[] sendB = new byte[1024];
            sendB=Encoding.UTF8.GetBytes(sendStr);
            //ns.Write(sendB, 0, sendB.Length);
            //ns.Flush();
            //ns.Close();
            socket.Send(sendB, sendB.Length, SocketFlags.None);
            socket.Close();
            Console.WriteLine("连接关闭");
            Console.ReadLine();
        }
    }
}
